package com.example.gokchinesefoodmappcdev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gokchinesefoodmappcdev.dao.MerchantInfoDao;
import com.example.gokchinesefoodmappcdev.dao.OrderInfoDao;
import com.example.gokchinesefoodmappcdev.entity.CustomerInfo;
import com.example.gokchinesefoodmappcdev.entity.MerchantInfo;
import com.example.gokchinesefoodmappcdev.entity.OrderInfo;
import com.example.gokchinesefoodmappcdev.entity.PageBean;
import com.example.gokchinesefoodmappcdev.service.MerchantInfoService;
import com.example.gokchinesefoodmappcdev.util.ResultData;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class MerchantInfoServiceImpl implements MerchantInfoService {
    @Autowired
    private MerchantInfoDao merchantInfoDao;
    @Autowired
    private OrderInfoDao orderInfoDao;
    /**
     * 添加商户
     * @param merchantInfo 商户信息
     * @return
     */
    @Override
    public ResultData insertMerchantInfo(MerchantInfo merchantInfo) {
        // 判断商户管理员帐号是否重复
        QueryWrapper<MerchantInfo> wrapper = new QueryWrapper();
        wrapper.eq("user_name",merchantInfo.getUserName());
        MerchantInfo merchant = merchantInfoDao.selectOne(wrapper);
        if (merchant != null) {
            return ResultData.fail(ResultData.OPERATION_FAIL,"商户管理员帐号不可重复");
        }
        // 添加商户
        // 设置添加时间
        Date date = new Date();
        merchantInfo.setGmtCreate(date);
        int i = merchantInfoDao.insert(merchantInfo);
        if (i > 0) {
            return ResultData.ok(ResultData.OPERATION_SUCCESS,"添加商户成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL,"添加商户失败");
    }

    /**
     * 分页查询
     * 根据条件查询所有用户
     * @param merchantInfo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<MerchantInfo> findMerchantInfoAll(MerchantInfo merchantInfo, int pageNum, int pageSize) {
        //使用PageHelper开启分页，该该方法调用需要在调用数据库的操作之前执行
        PageHelper.startPage(pageNum,pageSize);
        //查出数据结果集
        List<MerchantInfo> merchantInfoList = merchantInfoDao.findAllMerchanInfo(merchantInfo);
        //获取总数据数
        int merchanInfoNum = merchantInfoDao.countMerchanInfoNum(merchantInfo);
        //将信息封装到PageBean中，返回出去
        PageBean<MerchantInfo> pageBean = new PageBean<>(pageNum,pageSize,merchanInfoNum);
        pageBean.setRows(merchantInfoList);

        return pageBean;
    }

    /**
     * 根据id查询商户
     * @param id 商户id
     * @return
     */
    @Override
    public ResultData findMerchantInfoById(Integer id) {
        QueryWrapper<MerchantInfo> wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        MerchantInfo merchantInfo = merchantInfoDao.selectOne(wrapper);
        if (merchantInfo == null) {
            return ResultData.fail(ResultData.OPERATION_FAIL,"查询失败");
        }
        return ResultData.createData(ResultData.OPERATION_SUCCESS,merchantInfo);
    }

    /**
     * 更新商户
     * @param merchantInfo
     * @return
     */
    @Override
    public ResultData updateMerchantInfoById(MerchantInfo merchantInfo) {
        QueryWrapper<MerchantInfo> wrapper = new QueryWrapper();
        wrapper.eq("id",merchantInfo.getId());
        int i = merchantInfoDao.update(merchantInfo, wrapper);
        if (i > 0) {
            return ResultData.ok(ResultData.OPERATION_SUCCESS,"更新成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL,"更新失败");
    }

    /**
     * 批量删除商户
     * @param ids
     * @return
     */
    @Override
    public ResultData deleteMerchanInfoByIds(Integer[] ids) {
        List<Integer> idsList = Arrays.asList(ids);
        QueryWrapper<MerchantInfo> wrapper = new QueryWrapper();
        wrapper.in("id",idsList);
        int i = merchantInfoDao.delete(wrapper);
        if (i > 0) {
            return ResultData.ok(ResultData.OPERATION_SUCCESS,"删除成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL,"删除失败");
    }

    @Override
    public ResultData updateMerchanInfoStatusById(MerchantInfo merchantInfo) {
        // 查询用户是否有未完成的订单
        QueryWrapper<OrderInfo> orderInfoWrapper = new QueryWrapper();
        List<Integer> statusList = new ArrayList<>();
        statusList.add(0);
        statusList.add(3);
        statusList.add(-3);
        orderInfoWrapper.notIn("order_status",statusList);
        orderInfoWrapper.eq("merchant_id",merchantInfo.getId());
        List<OrderInfo> orderInfoList = orderInfoDao.selectList(orderInfoWrapper);
        if (!orderInfoList.isEmpty()) {
            return ResultData.fail(ResultData.OPERATION_FAIL,"该用户有未完成订单，暂时无法禁用！");
        }
        // 修改用户的状态
        QueryWrapper<MerchantInfo> merchantInfoWrapper = new QueryWrapper();
        merchantInfoWrapper.eq("id",merchantInfo.getId());
        int i = merchantInfoDao.update(merchantInfo, merchantInfoWrapper);
        if (i > 0) {
            return ResultData.ok(ResultData.OPERATION_SUCCESS,"修改成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL,"修改失败");
    }

    /**
     * 商户导出
     * @param merchantInfo
     * @return
     */
    @Override
    public List<MerchantInfo> MerchanInfoListToExcel(MerchantInfo merchantInfo) {
        List<MerchantInfo> merchantInfoList = merchantInfoDao.findAllMerchanInfo(merchantInfo);
        return merchantInfoList;
    }
}
